<?php

namespace app\common\controller;
use think\facade\Request;
use think\facade\Filesystem;
use app\common\model\AdminFile;
class Plugins
{

    public function Qrcode()
    {
        $text=input("text") ? : '123456';
        $PluginsQRcode = new \dh2y\qrcode\QRcode();
        $PluginsQRcode->png($text, false, 10, '', input("m"))->getPath();
        $img = ob_get_contents();
        ob_end_clean();
        echo $img;
    }
    public function Barcode()
    {
        header('Content-Type: image/png');
        $text=input("text") ? : '123456';
        $generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
        echo $generator->getBarcode($text, $generator::TYPE_CODE_128);
        $img = ob_get_contents();
        ob_end_clean();
        echo $img;
    }


    public function Upload()
    {
        $file = Request::file("file") ? : Request::file("edit");

        if (input("tag")=='markdown')
        {
            $file=Request::file("editormd-image-file");
        }

        $updata['size']=(int)$file->getSize()/1000;
        $updata['name'] = $file->getOriginalName();
        $updata['mime']=$file->getOriginalExtension();
        $d=count(explode($updata['mime'],GetConfig('system', 'file_mine')));
        if ($d<=1)
        {
            return BackCode([],203,'不被允许的文件类型');
        }
        if ($updata['size']>GetConfig('system', 'file_size'))
        {
            return BackCode([],203,'超过允许的文件大小');
        }
        $path =$file->getpathName();
        $updata['type']=input('type') ? : GetConfig('system', 'file_type');;
        $file_local=GetConfig('system', 'file_local');
        $updata['source_url']=!empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '无';
        switch ($updata['type'])
        {
            case 1://本地存储
                $updata['url'] ='/storage/'. Filesystem::disk('public')->putFile($file_local, $file);
                break;
            case 2://阿里云
                $url = Filesystem::disk('aliyun')->putFile($file_local, $file);
                $updata['url'] =GetConfig('system', 'aliyun_url').'/'.$url;
                break;
            case 3://七牛云
                $url = Filesystem::disk('qiniu')->putFile($file_local, $file);
                $updata['url'] =GetConfig('system', 'qiniu_url').'/'.$url;
                break;
            case 4://腾讯云
                $url= Filesystem::disk('qcloud')->putFile($file_local, $file);
                $updata['url'] =GetConfig('system', 'qcloud_url').'/'.$url;
                break;
            default:
        }
        $updata['url']=str_replace('\\','/',$updata['url']);
        $updata['create_time'] = date('Y-m-d H:i:s');
        $updata['create_id'] = GetToken('id') ?: 0;
        $Model = new AdminFile();
        $id=$Model->insertGetId($updata);
        if (input('tag')=='tinymce')
        {

            return json(['location'=>$updata['url']]);
        }
        if (input('tag')=='markdown')
        {

            return json(['url'=>$updata['url'],'success'=>1]);
        }


        $back=['id'=>$id,'location'=>$updata['url']];
        return BackCode($back);
    }

    public function UploadList()
    {
        return ViewTpl('common/plugins/uploadList');
    }

    public function Video()
    {
        return ViewTpl('common/plugins/video');
    }







}